<?php

/**
 * index
 * @author 刘军 
 * @version 1.0
 * @package application
 */
class Member_do extends Front_Controller
{
	public function __construct() {
		parent::__construct();
		$this->load->model('member_model');
	}
	
	//登陆
	public function login(){
		
		//判断是否已经登陆
		if($this->is_login()){
			header('location:' . base_url($this->_site_path . '/member/index'));
		}
		//登陆验证
		$user_data = $this->input->post();
		if($user_data['login']){
			$captcha_word = $this->session->userdata('captcha_word');
			if($captcha_word != strtoupper($user_data['vdcode'])){
				$this->error_js('验证码错误！');exit;
			}
			$attributes = array('user_name' => $user_data['user_name']);
			//检查用户名是否存在
			$total = $this->member_model->total($attributes);
			if(!$total){
				$this->error_js('用户名或密码有误！');exit;
			}
			$password =  $this->member_model->get_value_by_notpk('password', $attributes);
			//检查密码是否相等
			if($password != md5($user_data['password'])){
				$this->error_js('用户名或密码有误！');exit;
			}
			$user_data['user_id'] =  $this->member_model->get_value_by_notpk('user_id', $attributes);
			//设置cookie
			setcookie('user_name', $user_data['user_name'], time()+$user_data['keeptime']);
			setcookie('password', $user_data['password'], time()+$user_data['keeptime']);
			//设置session
			unset($user_data['login'], $user_data['vdcode'], $user_data['keeptime'], $user_data['password']);
			$this->session->set_userdata($user_data);
			//修改最后登陆时间
			$this->member_model->update_by_pk(array('last_login'=>time()), $user_data['user_id']);
			//跳转至首页
			header('location:' . base_url($this->_site_path . '/member/index'));
		}
		$data = array();
		$this->load->view_part($this->_site_path.'/member_do/login', $data);
	}
	
	//注册
	public function register(){
		
		$data = array();
		$captcha_word = $this->session->userdata('captcha_word');
		if(isset($captcha_word) && $_POST['register']=='1'){
			$user_data = $this->input->post();
			if($captcha_word != strtoupper($user_data['vdcode'])){
				echo '验证码错误！';exit;
			}
			if($user_data['agree'] != 1){
				echo '你必须同意注册协议！';exit;
			}
			$rule_config=array(
					array('field'=> 'user_name' , 'label'=> '用户名' , 'rules'=> 'required|max_length[50]'),
					array('field'=> 'unit_name' , 'label'=> '企业名称或个人昵称' , 'rules'=> 'required|max_length[50]'),
					array('field'=> 'password' , 'label'=> '密码' , 'rules'=> 'required|min_length[6]|matches[passconf]|md5'),
					array('field'=> 'passconf' , 'label'=> '确认密码' , 'rules'=> 'required'),
					array('field'=> 'email' , 'label'=> '邮箱' , 'rules'=> 'trim|required|valid_email'),
			);
			$this->load->library('form_validation');
			$this->form_validation->set_rules($rule_config);
			if ( $this->form_validation->run() ){
				unset($user_data['register'], $user_data['vdcode'], $user_data['agree'], $user_data['passconf']);
				//用户名不能含有“.”或“@”等敏感字符
				if(strpos($user_data['user_name'], '.')!==false || strpos($user_data['user_name'], '@')!==false){
					echo '用户名格式不正确！';exit;
				}
				//验证用户名是否唯一
				$total = $this->member_model->total(array('user_name' => $user_data['user_name']));
				if($total){
					echo '用户名已被注册！';exit;
				}
				//验证邮箱是否唯一
				$total = $this->member_model->total(array('email' => $user_data['email']));
				if($total){
					echo '邮箱已被注册！';exit;
				}
				$user_data['created'] = $user_data['modified'] = $user_data['last_login'] = time();
				$user_data['password'] = md5($user_data['password']);
				$member_id = $this->member_model->insert( $user_data );
				if(!$member_id){
					echo '注册失败！';exit;
				}
				//设置session
				$info = array('user_id' => $member_id, 'user_name'=>$user_data['user_name']);
				$this->session->set_userdata($info);
				echo base_url($this->_site_path . '/member/index');exit;
			}else{
				echo '资料有误！';exit;
			}
		}else{
			$this->load->model('member_safe_questions_model');
			$data['questions'] = $this->member_safe_questions_model->get_values('id', 'question');
			$this->load->view_part($this->_site_path.'/member_do/register', $data);
		}
	}
	
	//ajax注册验证用户名或邮箱是否唯一
	public function ajaxRegCheck(){
		
		$data = $this->input->post();
		if(isset($data['flag'])){
			switch ($data['flag']){
				case 'checkUser':
					$attributes = array('user_name' => $data['user_name']);
					$total = $this->member_model->total($attributes);
					echo $total ? '<b>×用户名已被注册！</b> ': 'ok';
					break;
				case 'checkEmail':
					$attributes = array('email' => $data['email']);
					$total = $this->member_model->total($attributes);
					echo $total ? '<b>×邮箱已被注册！</b> ' : 'ok';
					break;
				default:
					echo '<b>传入参数有误！</b>';
			}
		}
	}
	
	//退出
	public function logout(){
		
		$this->session->sess_destroy();
		header('location:/');
	}
}